package com.skycong.hello77.complexforms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.skycong.hello77.R;
import com.skycong.hello77.complexforms.entity.DataComparisonVO;
import com.skycong.hello77.complexforms.view.PanelListLayout;

import java.util.List;

/**
 * Created by Kiki on 2018/6/14.
 */

public class FormSecondAdapter extends AbstractPanelListAdapter {
    private List<DataComparisonVO> items;
    private OnSetOwnListener listener;

    public FormSecondAdapter(Context context, PanelListLayout pl_root, ListView lv_content, List<DataComparisonVO> items, TextView top_title,OnSetOwnListener listener){
        super(context, pl_root, lv_content,  items,  top_title);
        this.listener = listener;

    }

     @Override
    protected BaseAdapter getContentAdapter() {
        return new ContentAdapter(items);
    }
    //内容列表自定义布局
    public class ContentAdapter extends BaseAdapter{
        private List<DataComparisonVO> items;
           public ContentAdapter(List<DataComparisonVO> items) {
               this.items = items;
           }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //convertView不为空的时候直接重新使用convertView从而减少了很多不必要的View的创建，然后加载数据
            //最快的方式是定义一个ViewHolder，将convetView的tag设置为ViewHolder,不为空时重新使用即可
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_second,parent,false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.content1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        listener.firstMethod();
                    }
                }
            });
            return convertView;
        }

        /*
        * ViewHolder viewHolder;
        * if(contentView == null){
        *   contentView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_second,parent,false);
        *   viewHolder = new ViewHolder(contentView);
        *   contentView.setTag(viewHolder)
        * }else{
        *  viewHolder = contentView.getTag(viewHolder)
        * }
        * return contentView
        * */
    }
    class ViewHolder {
        private TextView content1;
        public ViewHolder(View view){
            content1 = (TextView) view.findViewById(R.id.id_tv_content1);
        }

    }
    public interface OnSetOwnListener{
        /**
         * 第一个方法
         */
        void firstMethod();

        /**
         * 第二次方法
         */
        void secondMethod();
    }
}
